home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_066 / sendpackets / asendpacket / asendpacket.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  195 lines

  1. /* asendpacket.c - (asynchronous)        send multiple packets to a dos-      */
  2. /* 05-SEP-86                                handler                           */
  3. /* Phillip Lindsay - Commodore-Amiga                                          */
  4.  
  5. #include "exec/types.h"
  6. #include "exec/ports.h"
  7. #include "exec/io.h"
  8. #include "exec/memory.h"
  9. #include "libraries/dos.h"
  10. #include "libraries/dosextens.h"
  11. #include <stdio.h>
  12. #include "functions.h"           /* aztec C include */
  13.  
  14.  
  15. #define DOSTRUE             -1L  /* AmigaDos TRUE */
  16. #define MAXARGS              7L  /* limit in packet structure (dosextens.h) */
  17.  
  18. /* 
  19.    asynchronous sendpkt routine 
  20.    you must supply a port for packet replies...This function returns the
  21.    address of the pending packet. 
  22.  
  23. */ 
  24. long asendpkt(replyport,pid,action,args,nargs)
  25.  
  26. struct MsgPort *replyport; /* where all packet replies are sent */
  27. struct MsgPort *pid;      /* process indentifier ... (handlers message port ) */
  28. long action,             /* packet type ... (what you want handler to do )   */
  29.      args[],            /* a pointer to a argument list */
  30.      nargs;            /* number of arguments in list  */
  31. {
  32.   
  33.  struct StandardPacket *packet;
  34.  
  35.  long   count, *pargs; 
  36.  
  37.  if(nargs > MAXARGS) return(NULL); 
  38.  
  39.  packet = (struct StandardPacket *) 
  40.    AllocMem((long)sizeof(*packet),MEMF_PUBLIC | MEMF_CLEAR);
  41.  if(!packet) 
  42.     return(NULL);
  43.  
  44.  packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt); /* link packet- */
  45.  packet->sp_Pkt.dp_Link         = &(packet->sp_Msg);        /* to message    */
  46.  packet->sp_Pkt.dp_Port         = replyport;         /* set-up reply port   */
  47.  packet->sp_Pkt.dp_Type         = action;           /* what to do... */
  48.  
  49.  /* move all the arguments to the packet */
  50.  pargs = &(packet->sp_Pkt.dp_Arg1);        /* address of first argument */
  51.  for(count=NULL;count < nargs;count++) 
  52.    pargs[count]=args[count];
  53.  
  54.  PutMsg(pid,packet); /* send packet */
  55.  
  56.  return((long)packet);   /* everything went ok...so far... give-em the packet */
  57.   
  58. }
  59.  
  60. /* end of asendpkt.c */
  61.  
  62.  
  63. /* 
  64.   
  65.    simple packet flush with error detection ... returns ZERO for error
  66.    or DOSTRUE (-1) for a-o-k.
  67.    
  68.    A packet error can be detected in most cases by "Res1" being equal
  69.    to zero--"Res2" will hold more information pertaining to the error. 
  70.    The problem with handling multiple packets is detecting an error for a 
  71.    particular packet. What makes things a little more difficult is the fact
  72.    that the "Res1" member of the packet structure is not generally an 
  73.    indicator of an error. So depending on what type of packets you'll be
  74.    handling you might want to deal with packet replies differently.
  75.  
  76. */
  77.  
  78. long pktflush(rport,pkts)
  79. struct MsgPort *rport;      /* reply port for packets     */
  80. long           pkts;        /* number of packets to flush */
  81.  
  82. {
  83.  struct StandardPacket *apkt;
  84.  long res1,cres;
  85.  
  86.  res1 = DOSTRUE;
  87.  
  88.  while(pkts--)                          /* received all packets? */
  89.   {
  90.    WaitPort(rport);                     /* sleep until a packet arrives */
  91.    apkt = (struct StandardPacket *) GetMsg(rport);       /* get packet */
  92.    cres = apkt->sp_Pkt.dp_Res1;         /* get result */
  93.    FreeMem(apkt,(long)sizeof(*apkt));   /* free packet structure from memory  */
  94.    res1 = (!cres ? cres : res1);        /* error?     */  
  95.   } 
  96.  
  97.  return(res1);
  98. }
  99.  
  100.   
  101. /* 
  102.    for all those people interested in implementing AmigaDos file I/O
  103.    with an asynchronous design.
  104. */
  105.  
  106.  
  107. /* start of example */
  108.  
  109. #define NARGS     3L                      /* number of arguments */
  110. #define NBUFFS   1L                      /* number of buffers   */
  111. #define BUFFLEN 60L                      /* buffer length       */
  112. #define ESC     27L
  113.  
  114. main()
  115. {
  116.  
  117.  struct MsgPort        *filehdlr;      /* for process id  handler  */
  118.  struct MsgPort        *rport;         /* where all packets return */
  119.  long                  arg[NARGS],     /* array of arguments       */
  120.                        rpkt,           /* holds returned packet    */
  121.                        count;          /* count messages           */ 
  122.  struct FileHandle     *filehandle;    /* our file handle          */
  123.  BPTR                  fh,             /* AmigaDos file handle     */
  124.                        fharg1;         /* Arg1 from filehandle     */
  125.  UBYTE                 *buff;          /* buffer pointer           */
  126.  struct StandardPacket *pkt;           /* our packet               */
  127.  
  128.  
  129. /* get buffers */
  130.  buff = (UBYTE *) AllocMem((BUFFLEN * NBUFFS),MEMF_PUBLIC | MEMF_CLEAR);
  131.  if(!buff) exit(TRUE);
  132.  
  133.  rport = (struct MsgPort *) CreatePort(NULL,NULL); /* make reply port */
  134.  if(!rport) 
  135.   { 
  136.    FreeMem(buff,(BUFFLEN * NBUFFS));
  137.    exit(TRUE);
  138.   }
  139.  
  140. /* here we open a dummy file */
  141.  fh = (BPTR) Open("RAM:temp",MODE_NEWFILE);
  142.  if(!fh) 
  143.   {
  144.    FreeMem(buff,(BUFFLEN * NBUFFS));
  145.    DeletePort(rport);
  146.    exit(TRUE);
  147.   }
  148.  
  149. /* bring our AmigaDos file handle into the real world... */
  150.  filehandle = (struct FileHandle *)  (fh << 2);
  151.  
  152. /* read your AmigaDOS Technical Reference Manual for packet requirements */
  153.  
  154.  fharg1     = filehandle->fh_Arg1;  /* get Arg1 */
  155.  
  156.  filehdlr   = filehandle->fh_Type;  /* get handler for file */
  157.  
  158. /* 
  159.    you could get process id of the handler this way also ...
  160.       filehdlr = (struct MsgPort *) DeviceProc("DF1:");
  161. */
  162.  
  163. /* give each buffer unique data */
  164.  
  165.  for(count=0;count < (BUFFLEN * NBUFFS);count ++)
  166.   buff[count]= 0x31 + (count / BUFFLEN);
  167.  
  168. /* set-up arguments and send packets */
  169.  
  170.  arg[0]= (long) fharg1;        /* file handle Arg1  */
  171.  arg[1]= (long) &buff[0];     /* buffer            */
  172.  arg[2]=        BUFFLEN;     /* buffer length     */
  173.  puts("The packet");
  174.  rpkt = asendpkt(rport,filehdlr,ACTION_WRITE,arg,NARGS);
  175.  puts("has been sent...");
  176.  
  177.  /* while the above i/o is happening you are free to do other things... */
  178.  
  179.  /* !!! YOU CANNOT HAVE MORE THAN ONE PACKET OUTSTANDING PER FILE !!! */
  180.  
  181. /* a more elegant packet flush routine would be nice */
  182.  
  183. if(!pktflush(rport,1L)) puts("Error in packet sent.");
  184.  
  185. /* done clean up... */
  186.  Close(fh);
  187.  FreeMem(buff,(BUFFLEN * NBUFFS));
  188.  DeletePort(rport);
  189.  exit(FALSE);
  190.  
  191. }
  192.  
  193. /* eof */
  194.  
  195.